package stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/25 13:37
 * @Copyright 博为峰
 */
public class StreamDemo {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("张三", 20, 96));
        list.add(new Student("李四", 30, 89));
        list.add(new Student("王五", 50, 73));
        list.add(new Student("赵六", 60, 86));
        list.add(new Student("王二", 10, 66));
        list.add(new Student("赵四", 26, 76));
        list.add(new Student("德刚", 45, 82));
        list.add(new Student("于谦", 43, 92));
        list.add(new Student("云鹏", 32, 80));
        list.add(new Student("云鹏", 32, 80));

//        m1(list);
//        m2(list);
//        m3(list);
//        m4(list);
//        m5(list);
//        m6(list);
//        m7(list);
//        m8(list);
//        m9(list);
//        m10(list);
    }

    //收集
    private static void m10(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
        List<Student> studentList = stream.filter(student -> student.getScore() >= 90).collect(Collectors.toList());
    }

    //统计
    private static void m9(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
        Stream<Integer> stream2 = stream.map(Student::getScore);
//        Optional<Integer> optional = stream2.reduce(new BinaryOperator<Integer>() {
//            @Override
//            public Integer apply(Integer integer, Integer integer2) {
//                return integer + integer2;
//            }
//        });
//        Optional<Integer> optional = stream2.reduce((i1, i2) -> i1 + i2);
        Optional<Integer> optional = stream2.reduce(Integer::sum);
        System.out.println(optional.get());
    }

    //分页
    private static void m8(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
        stream.skip(3).limit(3).forEach(System.out::println);
    }

    //排序
    private static void m7(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
//        stream.sorted(new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getScore() - o2.getScore();
//            }
//        }).forEach(System.out::println);

//        stream.sorted(((o1, o2) -> o1.getScore() - o2.getScore())).forEach(System.out::println);
//        Stream<Integer> stream2 = stream.map(new Function<Student, Integer>() {
//            @Override
//            public Integer apply(Student student) {
//                return student.getScore();
//            }
//        });
//        stream2 = stream.map(student->student.getScore());
        Stream<Integer> stream2 = stream.map(Student::getScore);
        stream2.sorted(Integer::compare).forEach(System.out::println);
//        stream2.sorted(Math::max).forEach(System.out::println);
    }

    //映射
    private static void m6(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
        /*stream.map(new Function<Student, Integer>() {
            @Override
            public Integer apply(Student student) {
                return student.getScore();
            }
        }).forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.println(integer);
            }
        });*/

//        stream.map(student -> student.getScore()).forEach(System.out::println);
        stream.map(Student::getScore).forEach(System.out::println);
    }

    //最值
    private static void m5(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
//        Optional<Student> optional = stream.max(new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
////                return o1.getScore() - o2.getScore();
//                return Integer.compare(o1.getScore(), o2.getScore());`
//            }
//        });
//        Optional<Student> optional = stream.max((o1, o2) -> o1.getScore() - o2.getScore());
        Stream<Integer> stream2 = stream.map(student -> student.getScore());
        Optional<Integer> optional = stream2.max(Integer::compare);
        System.out.println(optional.get());
    }

    //过滤
    private static void m4(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
        /*stream.filter(new Predicate<Student>() {
            @Override
            public boolean test(Student student) {
                return student.getScore() >= 90;
            }
        }).forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });*/
//        stream.filter(student -> student.getScore() >= 90).forEach(student -> System.out.println(student));
        stream.filter(student -> student.getScore() >= 90).forEach(System.out::println);
    }

    //去重
    private static void m3(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
        System.out.println(stream.distinct().count());
    }

    //总数
    private static void m2(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
        System.out.println(stream.count());
    }

    //遍历
    private static void m1(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();
//        stream.forEach(new Consumer<Student>() {
//            @Override
//            public void accept(Student student) {
//                System.out.println(student);
//            }
//        });
        //lambda表达式
//        stream.forEach(stu -> System.out.println(stu));
        //方法引用
        stream.forEach(System.out::println);
    }
}
